package com.clothes.act;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.clothes.R;
import com.clothes.app.BaseUrl;
import com.clothes.mdoel.ClothesTable;
import com.clothes.utils.DateTool;
import com.clothes.views.EditLinearLayout;
import com.clothes.views.pull.PullToRefreshLayout;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class BSearch extends BaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_num)
    EditText etNum;
    @Bind(R.id.tv_begin_time)
    TextView tvBeginTime;
    @Bind(R.id.tv_end_time)
    TextView tvEndTime;
    @Bind(R.id.iv_search)
    ImageView ivSearch;
    @Bind(R.id.ll_table)
    EditLinearLayout llTable;
    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshView;
    @Bind(R.id.tv_all_two)
    TextView tvAllTwo;
    @Bind(R.id.tv_all_four)
    TextView tvAllFour;
    private List<ClothesTable> list = new ArrayList<>();

    @Override
    public void initView() {
        setContentView(R.layout.activity_bsearch);
        tvTitle.setText("车工已交货统计");
    }

    @Override
    public void initData() {
        mImmersionBar.statusBarColor(R.color.colorPrimaryDark).init();
        tvBeginTime.setText("2018-01-01");
        tvEndTime.setText(DateTool.getStringDate(System.currentTimeMillis(),"yyyy-MM-dd"));
        setTime(3);
        //禁止上拉 下拉刷新
        refreshView.setPullDownEnable(false);
        refreshView.setPullUpEnable(false);
        llTable.setOnNumChangeListener(new EditLinearLayout.OnNumChangeListener() {
            @Override
            public void onNumChange(int position, double money) {
                list.get(position).setPrice(money);
                tvAllFour.setText(getAllMoney()+"元");
            }
        });

        refreshView.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                getHttp();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
            }
        });

        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.toString().equals("")){
                    refreshView.setPullDownEnable(false);
                }else {
                    refreshView.setPullDownEnable(true);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private int t;
    @Override
    protected void setTimeText(String time) {
        if(t == 1)
            tvBeginTime.setText(time);
        else
            tvEndTime.setText(time);
    }

    //请求网络 刷新
    private void getHttp(){
        String num = etNum.getText().toString();
        if(num.equals("")){
            showToast("请输入工号");
            return;
        }
        OkGo.<String>get(BaseUrl.BSerch)
                .params("workerNum",num)
                .params("beginSubmitDate",tvBeginTime.getText().toString())
                .params("endSubmitDate",tvEndTime.getText().toString()+" 23:59")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        refreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                        String res = response.body().toString();
                        parseStr(res);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        refreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                        showToast("服务器请求失败");
                    }
                });
    }

    @OnClick({R.id.iv_back, R.id.tv_begin_time, R.id.tv_end_time, R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_begin_time:
                t = 1;
                timePickerView.show();
                break;
            case R.id.tv_end_time:
                t= 2;
                timePickerView.show();
                break;
            case R.id.iv_search:
                getHttp();
                break;
        }
    }

    //获取总数量
    private int getAllNum(){
        int all = 0;
        for (ClothesTable table : list){
            all += table.getSumNum();
        }
        return all;
    }

    //获取总金额
    private double getAllMoney(){
        double all = 0;
        for (ClothesTable table : list){
            all += table.getPrice();
        }
        return all;
    }

    private void parseStr(String res){
        list.clear();
        try {
            JSONObject object = new JSONObject(res);
            JSONArray array = object.getJSONArray("data");
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                ClothesTable info = new ClothesTable();
                info.setModelNumber(obj.getString("modelNumber"));
                info.setSumNum(obj.getInt("sumNum"));
                list.add(info);
            }
            llTable.setList(list);
            tvAllTwo.setText(getAllNum()+"件");
            tvAllFour.setText(getAllMoney()+"元");

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
